//
//  NYFormLoginViewController.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2017/11/3.
//  Copyright © 2017年 niuwaketang. All rights reserved.
//

#import "NYFormLoginViewController.h"
#import "NYGetCountryCodeViewController.h"
#import "NYUpdateUserInfoViewController.h"
#import "NYUserLoginForm.h"
#import "NYRegisterViewController.h"
#import "NYFindPwdViewController.h"

@interface NYFormLoginViewController ()
@property (nonatomic, strong) FXFormController *formController;
@end

@implementation NYFormLoginViewController
- (void)presentOrPushInViewController:(UIViewController *)viewController
{
        if (viewController == nil) {
                return;
        }
        if (!viewController.allowsArbitraryPresenting) {
                return;
        }
        
        if (viewController.navigationController)
        {
                if ([viewController.navigationController.viewControllers.lastObject isKindOfClass:[NYFormLoginViewController class]]) {
                        return;
                }
                [viewController.navigationController pushViewController:self animated:YES];
        }
        else
        {
                if (viewController.presentedViewController && [viewController.presentedViewController isKindOfClass:[NYFormLoginViewController class]])
                {
                        return;
                }
                
                if ([viewController.presentedViewController isKindOfClass:[BaseNavigationViewController class]])
                {
                        BOOL res = [[(BaseNavigationViewController *)viewController.presentedViewController viewControllers].lastObject isKindOfClass:[NYFormLoginViewController class]];
                        if (res)
                        {
                                return;
                        }
                }
                BaseNavigationViewController *nav = [[BaseNavigationViewController alloc] initWithRootViewController:self];
                [viewController presentViewController:nav animated:YES completion:nil];
        }
}

- (void)viewDidLoad
{
        [super viewDidLoad];
        NYUserManager.country_code = @"86";
        @weakify(self);
        
        self.controllerView.actionBlock = ^(NYFormLoginViewClickType clickType, NYLoginType type, NSDictionary * _Nullable param, NYUserStringActionBlock  _Nullable contryCode) {
                @strongify(self);
                switch (clickType)
                {
                        case NYFormLoginViewClickTypeGetSmsCode:
                        {
                                [self getSMSCodeParams:param];
                        }
                                break;
                        case NYFormLoginViewClickTypeLoginClick:
                        case NYFormLoginViewClickTypeThirdLoginClick:
                        {
                                [self loginType:type params:param];
                        }
                                break;
                        
                        case NYFormLoginViewClickTypeGetCountryCode:
                        {
                                [self getCountryCode:contryCode];
                        }
                                break;
                        case NYFormLoginViewClickTypeFindPwd:
                        {
                                [self findPwd];
                        }
                                break;
                        case NYFormLoginViewClickTypeRegister:
                        {
                                [self registerItemDidClick:nil];
                        }
                                break;
                }
        };
}

- (void)registerItemDidClick:(QMUIButton *)btn
{
        NYRegisterViewController *vc = [[NYRegisterViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
}
- (void)configNavigationBar:(BaseNavigationBar *)navigationBar
{
        [super configNavigationBar:navigationBar];
        self.title = @"登录";
}

/// MARK: - Form
- (void)loginType:(NYLoginType)type params:(NSDictionary *)params
{
        [NYUserManager loginType:type param:params success:^(NYUserModel * _Nonnull user) {
                [NYTipClass showText:[NSString aPP_LoginSuccess]];
                [self loginSuccess];
        } failed:^(NSError * _Nonnull error) {
                [NYTipClass showText:error.localizedDescription inView:self.view];
        }];
}
- (void)loginSuccess
{
        if (self.isPresentedByOther)
        {
                [self dismissViewControllerAnimated:YES completion:self.dismissBlock];
                return;
        }
        
        if (!NYUserManager.isUserInfoComplete)
        {
                @weakify(self);
                [self routeUrl:kNYUpdateUserInfoControllerURI param:nil handler:^(NSDictionary<NSString *,id> * _Nullable parameters, UIViewController<JLRRouteDefinitionTargetController> * _Nullable targetVC) {
                        @strongify(self);
                        NSMutableArray *array = [NSMutableArray array];
                        [array addObject:self.navigationController.viewControllers.firstObject];
                        [array addObject:targetVC];
                        [self.navigationController setViewControllers:array animated:YES];
                }];
        }
        else
        {
                if (self.dismissBlock)
                {
                        self.dismissBlock();
                }
                NSMutableArray <UIViewController *>*array = [NSMutableArray arrayWithCapacity:self.navigationController.viewControllers.count];
                [self.navigationController.viewControllers enumerateObjectsUsingBlock:^(UIViewController *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if (![obj isKindOfClass:[self class]]) {
                                [array addObject:obj];
                        }
                }];
                [self.navigationController setViewControllers:array animated:YES];
        }
}
- (void)findPwd
{
        NYFindPwdViewController *vc = [[NYFindPwdViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
}
- (void)getSMSCodeParams:(NSDictionary *)params
{
        [NYUserManager getSMSWithParam:params success:^(NSString * _Nonnull sms) {
                
                [NYTipClass showText:sms inView:self.view];
                
        } failed:^(NSError * _Nonnull error) {
                [NYTipClass showText:error.localizedDescription inView:self.view];
        }];
}
- (void)getCountryCode:(NYUserStringActionBlock)contryCodeBlock
{
        NYGetCountryCodeViewController *vc = [[NYGetCountryCodeViewController alloc] init];
        [vc setGetCountryCodeBlock:^(NSString *countryCode) {
                if (contryCodeBlock)
                {
                        contryCodeBlock(countryCode);
                }
        }];
        [self.navigationController pushViewController:vc animated:YES];
}

@end
